﻿package client;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;

import server.ResBookInterface;
import server.ResCustomerInterface;
import server.ResDinnerInterface;
import server.ResWaiterInterface;

import java.awt.Dialog.ModalityType;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Date;


import client.Configuration;
import java.awt.Font;
import javax.swing.ImageIcon;


public class ResCustomServeGUI extends JDialog {

	private JPanel contentPane;
	private WhetherAppoint ch=new WhetherAppoint();
	
	
	 public static void main(String[] args){
		 EventQueue.invokeLater(new Runnable(){
			 	 
			public void run() {
				try {
					ResCustomServeGUI frame = new ResCustomServeGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
});
}



	public ResCustomServeGUI() {
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		//setModalityType(ModalityType.APPLICATION_MODAL);
		setBounds(100, 100, 504, 371);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton btnNewButton = new JButton("吃饭");
		btnNewButton.setFont(new Font("方正舒体", Font.PLAIN, 18));
		btnNewButton.setIcon(null);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				ch.go();
				ch.setModal(true);
				ch.setLocationRelativeTo(null);
				dispose();
				
				
			}
		});
		btnNewButton.setBounds(34, 46, 102, 23);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("\u9884\u8BA2");
		btnNewButton_1.setFont(new Font("方正舒体", Font.PLAIN, 18));
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
				ResBookInfoGUI bookInfo=new ResBookInfoGUI();
				bookInfo.setModal(true);
				bookInfo.setLocationRelativeTo(null);
				bookInfo.setVisible(true);
				
			}
		});
		btnNewButton_1.setBounds(34, 103, 102, 23);
		contentPane.add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("外卖");
		btnNewButton_2.setFont(new Font("方正舒体", Font.PLAIN, 18));
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ResTakeOutInfoGUI takeOut=new ResTakeOutInfoGUI();
				takeOut.setModal(true);
				takeOut.setLocationRelativeTo(null);
				takeOut.setVisible(true);
				}
		});
		btnNewButton_2.setBounds(34, 75, 102, 23);
		contentPane.add(btnNewButton_2);
		
	
		JLabel label = new JLabel("\u8BF7\u9009\u62E9\u4F60\u9700\u8981\u7684\u670D\u52A1");
		label.setFont(new Font("方正舒体", Font.BOLD, 21));
		label.setBounds(34, 10, 208, 26);
		contentPane.add(label);
		
		JButton btnBack = new JButton("back");
		btnBack.setIcon(null);
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
				ResEnterGUI res=new ResEnterGUI();
				res.setModal(true);
				res.setLocationRelativeTo(null);

				res.setVisible(true);
			
			}
		});
		btnBack.setBounds(421, 10, 57, 23);
		contentPane.add(btnBack);
		
		JLabel lblNewLabel = new JLabel("New label");
		lblNewLabel.setIcon(new ImageIcon("src/client/images/QQ截图未命名.png"));
		lblNewLabel.setBounds(0, -26, 495, 391);
		contentPane.add(lblNewLabel);
	}
}






class WhetherAppoint extends JDialog {

	private JPanel contentPane;


	public void go() {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					WhetherAppoint frame = new WhetherAppoint();
					frame.setLocationRelativeTo(null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public WhetherAppoint(){
		
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 281, 180);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("\u662F\u5426\u5DF2\u7ECF\u9884\u8BA2~");
		label.setBounds(50, 23, 200, 33);
		contentPane.add(label);
		
		

		
		
		JButton btnNewButton = new JButton("\u662F ");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				


				
				ResBookLoginGUI bookLogin;
				try {
					dispose();
					bookLogin = new ResBookLoginGUI();
					bookLogin.setModal(true);
					bookLogin.setLocationRelativeTo(null);
					bookLogin.setVisible(true);
			
			    	
				

				} catch (MalformedURLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (NotBoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
		btnNewButton.setBounds(29, 91, 93, 23);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("\u5426");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				Configuration config= new Configuration();

				String rmiAddress=config.getRmiAddress();
				
				try {
			//		ResCustomerInterface customer= (ResCustomerInterface) Naming.lookup(rmiAddress+"ResCustomer");
					ResWaiterInterface waiter= (ResWaiterInterface) Naming.lookup(rmiAddress+"ResWaiter");
					ResDinnerInterface dinner=(ResDinnerInterface)Naming.lookup(rmiAddress+"ResDinnerInfo");
			//
					
			//	System.out.print(waiter.searchWaiter());
					dispose();
					int waiterNumber=waiter.searchWaiter();
				String i=""+waiterNumber;
				String inputValue = JOptionPane.showInputDialog("请输入就餐人数");
				if(inputValue.equals("")){
					JOptionPane.showMessageDialog(null, "信息错误，请重新填写");
				}
				else
				{
					int a = Integer.parseInt(inputValue);
					while(a<=0||a>25){
						JOptionPane.showMessageDialog(null, "您的人数填写有误请重新填写");
						String input = JOptionPane.showInputDialog("请输入就餐人数");
						a = Integer.parseInt(input);
						
					}
					int t=dinner.addDinnerNumber();
					dinner.setDinnerPeople(t, a);
					
					
					Date day=new Date();
					int tableNumber=waiter.searchEmptyTable(a);
					if(tableNumber==0)
					{
						JOptionPane.showMessageDialog(null, i+"抱歉！已经没有您需要的桌子");
					}else{
					String table =""+tableNumber;
					dinner.setDinnerDate(t, day);
					dinner.setDinnerTable(t, tableNumber);
					
					JOptionPane.showMessageDialog(null, "服务员为您服务~\n请到"+table+"号桌就餐\n请点餐");
					
					ResMenuGUI menu=new ResMenuGUI(1,t);
					menu.setModal(true);
					menu.setLocationRelativeTo(null);
					menu.setVisible(true);
					}
									
				}
				
				
				} catch (MalformedURLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (NotBoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				
				
			}
		});
		btnNewButton_1.setBounds(156, 91, 93, 23);
		contentPane.add(btnNewButton_1);
		
		

	}
}
